/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pv168.uloha3.swing.workers;

import cz.muni.fi.pv168.uloha3.common.DBUtils;
import cz.muni.fi.pv168.uloha3.entities.RealEstate;
import cz.muni.fi.pv168.uloha3.exceptions.RealEstateManagerException;
import cz.muni.fi.pv168.uloha3.managers.RealEstateManager;
import cz.muni.fi.pv168.uloha3.managers.impl.RealEstateManagerImpl;
import cz.muni.fi.pv168.uloha3.swing.EstatesTableModel;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sql.DataSource;
import javax.swing.JOptionPane;
import javax.swing.SwingWorker;

/**
 *
 * @author stefan
 */
public class EstateDeleteSwingWorker extends SwingWorker<Boolean, Object> {
    
    private final RealEstate estate;
    private final EstatesTableModel model;

    public EstateDeleteSwingWorker(RealEstate estate, EstatesTableModel model) {
        this.estate = estate;
        this.model = model;
    }

    @Override
    protected Boolean doInBackground() throws Exception {
        DataSource dataSource = DBUtils.initConnector();
        
        RealEstateManager managerEstate = new RealEstateManagerImpl();
        managerEstate.setDataSource(dataSource);
        
        try {
            managerEstate.deleteRealEstate(estate);
        } catch(RealEstateManagerException ex) {
            Logger.getLogger(OwnerDeleteSwingWorker.class.getName()).log(Level.WARNING, ex.getMessage());
            JOptionPane.showMessageDialog(null, "Estate could not be deleted (" + ex.getMessage() + ")");
            return false;
        }
        
        model.removeEstate(estate);
        JOptionPane.showMessageDialog(null, "Estate deleted");
        return true;
    }
    
}
